#!/usr/bin/python
# -*-coding:utf-8-*-
# pip install ddddocr 图片识别
"""农产品质量追溯管理平台"""
import math
import time
import re
import requests
import json
from bs4 import BeautifulSoup
from time import sleep
import common
import db
import pymongo
import sys
import random
from dotenv import load_dotenv, find_dotenv
import os
import asyncio
from pyppeteer import launch
import numpy as np
import pandas as pd
import os

mysqldb = db.DbManager()

load_dotenv(verbose=True)
# 今天
today_time = common.TodayTime()
# 将-替换为.
today_time_new = today_time.replace('-', '.')
# 账号
username = os.getenv('LOGIN_ADMIN')
passworld = os.getenv('LOGIN_PWD')

# 获取登录后cookie
async def get_cookie(page):
    cookies_list = await page.cookies()
    cookies = ''
    for cookie in cookies_list:
        str_cookie = '{0}={1};'
        str_cookie = str_cookie.format(cookie.get('name'), cookie.get('value'))
        cookies += str_cookie
    print(cookies)
    return cookies


async def login():
    root_path="/Users/wankaifa/PycharmProjects/新思路/jsncpaq"
    log_url = "http://zs.jsncpaq.com/"
    # userDataDir:记录上网记录 是否已登录等。。 userDataDir='./userdata',
    # browser = await launch(headless=True, userDataDir='./userdata',args=['--no-sandbox'])
    browser = await launch(headless=True, args=['--disable-infobars', '--no-sandbox'])
    page = await browser.newPage()
    await page.setUserAgent(
        "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.137 Safari/537.36 LBBROWSER")
    await page.setViewport(viewport={'width': 1536, 'height': 768})
    await page.evaluateOnNewDocument('() =>{ Object.defineProperties(navigator,'
                                     '{ webdriver:{ get: () => false } }) }')

    res = await page.goto(log_url)
    # 屏蔽检测
    # await page_evaluate(page)
    # 等待页面加载完毕
    # await page.waitForXPath('/html/body/div[2]/div/div/div[1]/div/div')
    # 查看title 是否等于
    user_info = await page.title()
    print(user_info)
    if "农产品" not in user_info:
        print("爬取失败，请联系管理员")
        exit()
    print(str(user_info) + " 检查是否已登录")
    login_text = await page.querySelectorAllEval('#supplier', 'nodes=>nodes.map(node=>node.innerText)')
    print("到这边")
    if str(login_text[0]) == "登录":
        # 点击登录
        await page.type(
            'body > div.login-bd > div > div > div.lg-zone > div > form > div.lg-username.input-item.clearfix > input',
            username)
        await page.type(
            'body > div.login-bd > div > div > div.lg-zone > div > form > div.lg-password.input-item.clearfix > input',
            passworld, {'delay': 120})
        # 识别图片二维码
        print("开始识别图片验证码")
        # 标签截图 不删
        img = await page.querySelector('#canvas')
        await img.screenshot(path=root_path+'/CODE_IMG.png')
        import ddddocr
        ocr = ddddocr.DdddOcr(old=True)

        with open(root_path+"/CODE_IMG.png", 'rb') as f:
            image = f.read()
        # 识别后的验证码
        res = ocr.classification(image)
        # 替换 res
        if "u" in res:
            res = res.replace('u', '5')
        if "o" in res:
            res = res.replace('o', '0')
        if "l" in res:
            res = res.replace('l', '1')
        if "i" in res:
            res = res.replace('i', '1')
        print('识别出的验证码为：' + res)
        sleep(0.5)
        await page.type('#img-input', res, {'delay': 100})
        sleep(1)
        # 点击登录
        await page.click("#supplier")
        sleep(0.5)
        sleep(1)
        while True:
            try:
                login_text = await page.querySelectorAllEval('#supplier', 'nodes=>nodes.map(node=>node.innerText)')
            except Exception as e:
                print("验证码识别成功.")
                break
            if str(login_text[0]) == "登录":
                print("验证码验证失败,刷新验证码")
                await page.evaluate('document.querySelector("#img-input").value=""')
                await page.click("#canvas")
                await img.screenshot(path=root_path+'/CODE_IMG.png')
                with open(root_path+"/CODE_IMG.png", 'rb') as f:
                    image = f.read()
                # 识别后的验证码
                res = ocr.classification(image)
                # 替换 res
                if "u" in res:
                    res = res.replace('u', '5')
                if "o" in res:
                    res = res.replace('o', '0')
                if "l" in res:
                    res = res.replace('l', '1')
                if "i" in res:
                    res = res.replace('i', '1')
                print('识别出的验证码为：' + res)
                sleep(0.5)
                await page.type('#img-input', res, {'delay': 100})
                # 点击登录
                await page.click("#supplier")
                sleep(1)
            else:
                print("验证码识别成功,跳出循环")
                break
    # 创建一个DataFrame，用于保存到excel中
    df = pd.DataFrame(columns=('状态', '上传测速仪', '以检测数量', '总数量', '区域',
                               '样品名称', '被检测单位', '检测单位', '抽样人员',
                               '采样时间'))
    await page.goto("https://zs.jsncpaq.com/jg/mainManagement.html")
    sleep(3)
    # 获取js cookile
    js_page_cookie = await page.evaluate("""
                                            function get_cookie(){
                                            return document.cookie}
                                            """)
    # 获取并保存cookies
    page_cookie = await page.cookies()
    cookies = ''
    for cookie in page_cookie:
        str_cookie = '{0}={1};'
        str_cookie = str_cookie.format(cookie.get('name'), cookie.get('value'))
        cookies += str_cookie
    new_cookies = js_page_cookie + ";" + cookies
    print(new_cookies)
    if 'zsToken' in js_page_cookie:
        arr_token = js_page_cookie.split('zsToken=')
        token = arr_token[1].split('; ')

    List = {'cookie': js_page_cookie, 'token': token[0]}
    with open(root_path+"/cookie.txt", 'w') as file:
        [file.write("{}&&".format(List[i])) for i in List]

    # 切换iframe框代码
    await page.waitFor('#pro1 > div.searchBox > form > div:nth-child(4) > div > select')
    await page.select('#pro1 > div.searchBox > form > div:nth-child(4) > div > select', '1');  # 选择下拉框内容
    sleep(2)
    await page.select('#pro1 > div.tableBox.table-responsive > div > div.footer-toolbar > div.change-size > select', '20');  # 选择下拉框内容

    # 点击查询
    await page.click("#search1")
    sleep(5)
    login_text = await page.querySelectorAllEval(
        '#proTable1>tbody>tr',
        'nodes=>nodes.map(node=>node.innerHTML)')
    for new_page in range(1,2):
        print("第"+str(new_page)+"页")
        for i in range(len(login_text)):
            i=i+1
            print("第"+str(i)+"行")
            # 所属区域
            area_arr=await page.querySelectorAllEval('#proTable1 > tbody > tr:nth-child('+str(i)+') > td:nth-child(7) > span',
                                                     'nodes=>nodes.map(node=>node.innerText)')
            new_area_arr = area_arr[0].split('»')
            orgId_arr=await page.querySelectorAllEval(
                '#proTable1 > tbody > tr:nth-child('+str(i)+') > td:nth-child(11)',
                'nodes=>nodes.map(node=>node.innerHTML)')
            print(orgId_arr)
            new_orgId = orgId_arr[0].split('orgId=')
            orgId_ok = new_orgId[1].split('"')[0]
            print(orgId_ok)
            # 注册时间
            reg_arr = await page.querySelectorAllEval(
                '#proTable1 > tbody > tr:nth-child(' + str(i) + ') > td:nth-child(9)',
                'nodes=>nodes.map(node=>node.innerText)')
            reg_time = reg_arr[0]
            print(reg_time)
            try:
                info = {"orgId": orgId_ok, "area_str": area_arr[0], "register_time": reg_time,
                        'province_name': new_area_arr[0].strip(), 'city_name': new_area_arr[1].strip(),
                        'area_name': new_area_arr[2].strip(), 'street_name': new_area_arr[3].strip()}
            except Exception as e:
                print(area_arr)
                info = {"orgId": orgId_ok, "register_time": reg_time}
            find_info = mysqldb.table_select_one(table="main_info", field="id", where={"orgId": orgId_ok})
            # 存在数据则跳过
            if find_info:
                print(str(orgId_ok) + " 已存在数据 跳过")
                continue
            mysqldb.table_insert(table="main_info", data=info)
        print("点击下一页")
        await page.click("#pro1 > div.tableBox.table-responsive > div > div.footer-toolbar > div.ajax-page > ul:nth-child(3) > li.next-page")
        sleep(8)
    # 关闭
    await page.close()
    await browser.close()
    exit()


class Egas:
    def __init__(self):
        self.url = "http://zs.jsncpaq.com/"
        self.main()

    def main(self):
        # 登录
        asyncio.get_event_loop().run_until_complete(login())

Egas()
